package Model;

import java.awt.Point;
import java.util.ArrayList;

import Model.*;
import Model.Pieces.*;


public class ChessboardMoves_utils {

	/*public static ArrayList<Point> getPossibleMoves(Chessboard cb, Piece p)
	{
		ArrayList<Point> result = new ArrayList<Point>();
		boolean cropTop = false;
		boolean cropBottom = false;
		boolean cropLeft = false;
		boolean cropRight = false;
		boolean cropTopLeft = false;
		boolean cropTopRight = false;
		boolean cropBottomLeft = false;
		boolean cropBottomRight = false;

		if(p instanceof Bishop)
		{
			for(int i=1 ; i<8 ; i++)
			{
				if(p.getX()+i < 8 && p.getY()+i < 8 && !cropBottomLeft)
				{
					if(cb.getPiece(new Point(p.getX()+i, p.getY()+i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+i, p.getY()+i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()+i, p.getY()+i));
							cropBottomLeft = true;
						}
					}else{
						result.add(new Point(p.getX()+i, p.getY()+i));
					}
				}
				if(p.getX()+i < 8 && p.getY()-i >= 0 && !cropBottomRight)
				{
					if(cb.getPiece(new Point(p.getX()+i, p.getY()-i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+i, p.getY()-i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()+i, p.getY()-i));
							cropBottomRight = true;
						}
					}else{
						result.add(new Point(p.getX()+i, p.getY()-i));
					}
				}
				if(p.getX()-i >= 0 && p.getY()+i < 8 && !cropTopLeft)
				{
					if(cb.getPiece(new Point(p.getX()-i, p.getY()+i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()-i, p.getY()+i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()-i, p.getY()+i));
							cropTopLeft = true;
						}
					}else{
						result.add(new Point(p.getX()-i, p.getY()+i));
					}
				}
				if(p.getX()-i >= 0 && p.getY()-i >= 0 && !cropTopRight)
				{
					if(cb.getPiece(new Point(p.getX()-i, p.getY()-i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()-i, p.getY()-i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()-i, p.getY()-i));
							cropTopRight = true;
						}
					}else{
						result.add(new Point(p.getX()-i, p.getY()-i));
					}
				}
			}
		}
		else if(p instanceof King)
		{
			///////////////////////////////////
			// Penser a la mise en echec !!!!!!!!!!!!!
			///////////////////////////////////
			
			ArrayList<Point> list = new ArrayList<Point>();
			list.add(new Point(-1,-1));
			list.add(new Point(-1,0));
			list.add(new Point(-1,1));
			list.add(new Point(0,-1));
			list.add(new Point(0,1));
			list.add(new Point(1,-1));
			list.add(new Point(1,0));
			list.add(new Point(1,1));
			
			for(Point pt : list)
			{
				if(p.getX()+pt.x >= 0 && p.getX()+pt.x < 8 && p.getY()+pt.y >= 0 && p.getY()+pt.y < 8)
				{
					if(cb.getPiece(new Point(p.getX()+pt.x, p.getY()+pt.y)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+pt.x, p.getY()+pt.y)).getColor() != p.getColor())
							result.add(new Point(p.getX()+pt.x, p.getY()+pt.y));
					}else{
						result.add(new Point(p.getX()+pt.x, p.getY()+pt.y));
					}
				}
			}
		}
		else if(p instanceof Knight)
		{
			ArrayList<Point> list = new ArrayList<Point>();
			list.add(new Point(-2,-1));
			list.add(new Point(-1,-2));
			list.add(new Point(1,-2));
			list.add(new Point(2,-1));
			list.add(new Point(-2,1));
			list.add(new Point(-1,2));
			list.add(new Point(1,2));
			list.add(new Point(2,1));
			
			for(Point pt : list)
			{
				if(p.getX()+pt.x >= 0 && p.getX()+pt.x < 8 && p.getY()+pt.y >= 0 && p.getY()+pt.y < 8)
				{
					if(cb.getPiece(new Point(p.getX()+pt.x, p.getY()+pt.y)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+pt.x, p.getY()+pt.y)).getColor() != p.getColor())
							result.add(new Point(p.getX()+pt.x, p.getY()+pt.y));
					}else{
						result.add(new Point(p.getX()+pt.x, p.getY()+pt.y));
					}
				}
			}
		}
		else if(p instanceof Pawn)
		{
			int direction = 1;
			if(p.getColor() == Color.White) direction = -1;
			if(p.getNbTurns() == 0 && cb.getPiece(new Point(p.getX(), p.getY()+2*direction)) == null)
			{
				result.add(new Point(p.getX(), p.getY()+2*direction));
			}
			if(cb.getPiece(new Point(p.getX(), p.getY()+1*direction)) == null)
			{
				result.add(new Point(p.getX(), p.getY()+1*direction));
			}
			if(p.getColor() == Color.White)
			{
				if(p.getX()+1 < 8 && p.getY()-1 >= 0)
				{
					if(cb.getPiece(new Point(p.getX()+1, p.getY()-1)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+1, p.getY()-1)).getColor() != p.getColor())
							result.add(new Point(p.getX()+1, p.getY()-1));
					}
					if(cb.getPiece(new Point(p.getX()-1, p.getY()-1)) != null)
					{
						if(cb.getPiece(new Point(p.getX()-1, p.getY()-1)).getColor() != p.getColor())
							result.add(new Point(p.getX()-1, p.getY()-1));
					}
				}
				
			}else{
				if(p.getX()+1 < 8 && p.getY()+1 < 8)
				{
					if(cb.getPiece(new Point(p.getX()+1, p.getY()+1)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+1, p.getY()+1)).getColor() != p.getColor())
							result.add(new Point(p.getX()+1, p.getY()+1));
					}
				}
				if(p.getX()-1 >=0 && p.getY()-1 >=0)
				{
					if(cb.getPiece(new Point(p.getX()-1, p.getY()+1)) != null)
					{
						if(cb.getPiece(new Point(p.getX()-1, p.getY()-1)).getColor() != p.getColor())
							result.add(new Point(p.getX()-1, p.getY()-1));
					}
				}
			}
			
				
		}
		else if(p instanceof Queen)
		{

			for(int i=1 ; i<8 ; i++)
			{
				// DIAGONAL
				if(p.getX()+i < 8 && p.getY()+i < 8 && !cropBottomLeft)
				{
					if(cb.getPiece(new Point(p.getX()+i, p.getY()+i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+i, p.getY()+i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()+i, p.getY()+i));
							cropBottomLeft = true;
						}
					}else{
						result.add(new Point(p.getX()+i, p.getY()+i));
					}
				}
				if(p.getX()+i < 8 && p.getY()-i >= 0 && !cropBottomRight)
				{
					if(cb.getPiece(new Point(p.getX()+i, p.getY()-i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()+i, p.getY()-i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()+i, p.getY()-i));
							cropBottomRight = true;
						}
					}else{
						result.add(new Point(p.getX()+i, p.getY()-i));
					}
				}
				if(p.getX()-i >= 0 && p.getY()+i < 8 && !cropTopLeft)
				{
					if(cb.getPiece(new Point(p.getX()-i, p.getY()+i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()-i, p.getY()+i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()-i, p.getY()+i));
							cropTopLeft = true;
						}
					}else{
						result.add(new Point(p.getX()-i, p.getY()+i));
					}
				}
				if(p.getX()-i >= 0 && p.getY()-i >= 0 && !cropTopRight)
				{
					if(cb.getPiece(new Point(p.getX()-i, p.getY()-i)) != null)
					{
						if(cb.getPiece(new Point(p.getX()-i, p.getY()-i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()-i, p.getY()-i));
							cropTopRight = true;
						}
					}else{
						result.add(new Point(p.getX()-i, p.getY()-i));
					}
				}
				
				// HORIZONTAL + VERTICAL
				if(p.getY()-i >= 0 && !cropTop)
				{
					if(cb.getPiece(new Point(p.getX(), p.getY()-i)) != null)
					{
						if(cb.getPiece(new Point(p.getX(), p.getY()-i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX(), p.getY()-i));
							cropTop = true;
						}
					}else{
						result.add(new Point(p.getX(), p.getY()-i));
					}
				}
				if(p.getY()+i < 8 && !cropBottom)
				{
					if(cb.getPiece(new Point(p.getX(), p.getY()+i)) != null)
					{
						if(cb.getPiece(new Point(p.getX(), p.getY()+i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX(), p.getY()+i));
							cropBottom = true;
						}
					}else{
						result.add(new Point(p.getX(), p.getY()+i));
					}
				}
				if(p.getX()-i >= 0 && !cropLeft)
				{
					if(cb.getPiece(new Point(p.getX()-i, p.getY())) != null)
					{
						if(cb.getPiece(new Point(p.getX()-i, p.getY())).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()-i, p.getY()));
							cropLeft = true;
						}
					}else{
						result.add(new Point(p.getX()-i, p.getY()));
					}
				}
				if(p.getX()+i < 8 && !cropRight)
				{
					if(cb.getPiece(new Point(p.getX()+i, p.getY())) != null)
					{
						if(cb.getPiece(new Point(p.getX()+i, p.getY())).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()+i, p.getY()));
							cropRight = true;
						}
					}else{
						result.add(new Point(p.getX()+i, p.getY()));
					}
				}
			}
		}
		else if(p instanceof Tower)
		{
			for(int i=1 ; i<8 ; i++)
			{
				if(p.getY()-i >= 0 && !cropTop)
				{
					if(cb.getPiece(new Point(p.getX(), p.getY()-i)) != null)
					{
						if(cb.getPiece(new Point(p.getX(), p.getY()-i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX(), p.getY()-i));
							cropTop = true;
						}
					}else{
						result.add(new Point(p.getX(), p.getY()-i));
					}
				}
				if(p.getY()+i < 8 && !cropBottom)
				{
					if(cb.getPiece(new Point(p.getX(), p.getY()+i)) != null)
					{
						if(cb.getPiece(new Point(p.getX(), p.getY()+i)).getColor() != p.getColor())
						{
							result.add(new Point(p.getX(), p.getY()+i));
							cropBottom = true;
						}
					}else{
						result.add(new Point(p.getX(), p.getY()+i));
					}
				}
				if(p.getX()-i >= 0 && !cropLeft)
				{
					if(cb.getPiece(new Point(p.getX()-i, p.getY())) != null)
					{
						if(cb.getPiece(new Point(p.getX()-i, p.getY())).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()-i, p.getY()));
							cropLeft = true;
						}
					}else{
						result.add(new Point(p.getX()-i, p.getY()));
					}
				}
				if(p.getX()+i < 8 && !cropRight)
				{
					if(cb.getPiece(new Point(p.getX()+i, p.getY())) != null)
					{
						if(cb.getPiece(new Point(p.getX()+i, p.getY())).getColor() != p.getColor())
						{
							result.add(new Point(p.getX()+i, p.getY()));
							cropRight = true;
						}
					}else{
						result.add(new Point(p.getX()+i, p.getY()));
					}
				}
			}
		}
		return result;
		*/
		
		public static ArrayList<Point> getPossibleMoves(Chessboard cb, Piece p)
		{
			ArrayList<Point> result = new ArrayList<Point>();
			
			if(p instanceof Knight)
			{
				ArrayList<Point> list = new ArrayList<Point>();
				list.add(new Point(-2,-1));
				list.add(new Point(-1,-2));
				list.add(new Point(1,-2));
				list.add(new Point(2,-1));
				list.add(new Point(-2,1));
				list.add(new Point(-1,2));
				list.add(new Point(1,2));
				list.add(new Point(2,1));
				
				for(Point pt : list)
				{
					if(p.getX()+pt.x >= 0 && p.getX()+pt.x < 8 && p.getY()+pt.y >= 0 && p.getY()+pt.y < 8)
					{
						if(cb.getPiece(new Point(p.getX()+pt.x, p.getY()+pt.y)) != null)
						{
							if(cb.getPiece(new Point(p.getX()+pt.x, p.getY()+pt.y)).getColor() != p.getColor())
								result.add(new Point(p.getX()+pt.x, p.getY()+pt.y));
						}else{
							result.add(new Point(p.getX()+pt.x, p.getY()+pt.y));
						}
					}
				}
			}else if(p instanceof Pawn){
				int direction = 1;
				if(p.getColor() == Color.White) direction = -1;
				if(p.getNbTurns() == 0 && cb.getPiece(new Point(p.getX(), p.getY()+2*direction)) == null)
				{
					result.add(new Point(p.getX(), p.getY()+2*direction));
				}
				if(cb.getPiece(new Point(p.getX(), p.getY()+1*direction)) == null)
				{
					result.add(new Point(p.getX(), p.getY()+1*direction));
				}
				if(p.getColor() == Color.White)
				{
					if(p.getX() < 8 && p.getY()-1 >= 0)
					{
						if(cb.getPiece(new Point(p.getX()+1, p.getY()-1)) != null)
						{
							if(cb.getPiece(new Point(p.getX()+1, p.getY()-1)).getColor() != p.getColor())
								result.add(new Point(p.getX()+1, p.getY()-1));
						}
						if(cb.getPiece(new Point(p.getX()-1, p.getY()-1)) != null)
						{
							if(cb.getPiece(new Point(p.getX()-1, p.getY()-1)).getColor() != p.getColor())
								result.add(new Point(p.getX()-1, p.getY()-1));
						}
					}
					
				}else{
					if(p.getX()+1 < 8 && p.getY()+1 < 8)
					{
						if(cb.getPiece(new Point(p.getX()+1, p.getY()+1)) != null)
						{
							if(cb.getPiece(new Point(p.getX()+1, p.getY()+1)).getColor() != p.getColor())
								result.add(new Point(p.getX()+1, p.getY()+1));
						}
					}
					if(p.getX()-1 >=0 && p.getY()+1 < 8)
					{
						if(cb.getPiece(new Point(p.getX()-1, p.getY()+1)) != null)
						{
								if(cb.getPiece(new Point(p.getX()-1, p.getY()+1)).getColor() != p.getColor())
									result.add(new Point(p.getX()-1, p.getY()+1));
						}
					}
				}
			}else{
				for(int dirX=-1 ; dirX<2 ; dirX++)
				{
					for(int dirY=-1 ; dirY<2 ; dirY++)
					{
						
						
						if(!(dirY==0 && dirX==0)){
							boolean hasBreaked = false;
							for(Point itPt : p.getMovesPossibilities(dirX, dirY))
							{
								if(cb.getPiece(itPt)== null)
								{
									result.add(itPt);
								}
								else
								{
									if(cb.getPiece(itPt).getColor() != p.getColor())
									{
										result.add(itPt);
									}
									break;
								}
							}
						}
					}
				}
			}
			return result;
			
		}
}
